import { IWorld } from '../common/world.interface';
import { LocationComponent } from '../components/location.component';
import { MovementComponent } from '../components/movement.component';
import { System } from './system';

export class MovementSystem extends System {
  onUpdate(world: IWorld, dt: number) {
    const entities = world.findEntitiesByComponents([LocationComponent, MovementComponent]);

    entities.forEach((entity) => {
      const locationComponent = world.findComponentByEntity(entity, LocationComponent)!;
      const movementComponent = world.findComponentByEntity(entity, MovementComponent)!;

      locationComponent.location = locationComponent.location.add(movementComponent.velocity.multiply(dt));
    });
  }
}
